package com.exchangeek.server.service.mysql;

import com.exchangeek.server.entity.bean.User;
import com.exchangeek.server.service.definition.UserInfoServiceDefinition;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Project Name: Exchangeek_Server
 * Date Created: 2013/09/14 下午4:02
 *
 * @author Wings
 */

public class UserInfoDatabaseService extends DatabaseService implements UserInfoServiceDefinition {

    private static final String Table_Name = "users";
    private static final String SQL_CreateUser =
            "INSERT INTO " + Table_Name + " (userName,passHash,skills) VALUES ('%s','%s','%s');";
    private static final String SQL_QueryUser =
            "SELECT * FROM " + Table_Name + " WHERE userName='%s' AND passHash='%s';";
    private static final String SQL_UpdateUser =
            "UPDATE " + Table_Name + " SET userName='%s',passHash='%s',skills='%s' WHERE userName='%s' AND passHash='%s';";
    private static final String SQL_DeleteUser =
            "SELECT * FROM " + Table_Name + " WHERE userName='%s' AND passHash='%s';";

    public UserInfoDatabaseService(String databaseName) {
        super(databaseName);
    }

    @Override
    public Object createUser(User user) {
        String sql = String.format(SQL_CreateUser,
                user.getUserName(),
                user.getPassHash(),
                user.jsonGetSkills());
        try {
            this.databaseStatement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        return user.getUserName();
    }

    @Override
    public User queryUser(String userName, String passHash) {
        String sql = String.format(SQL_QueryUser, userName, passHash);
        try {
            ResultSet resultSet = this.databaseStatement.executeQuery(sql);
            resultSet.next();
            return new User()
                    .setUserName(resultSet.getString("userName"))
                    .setPassHash("(SECRET)")
                    .jsonSetSkills(resultSet.getString("skills"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Object updateUser(String oldUserName, String oldHash, User user) {
        //不允许更改用户名
        String sql = String.format(SQL_UpdateUser, oldUserName, user.getPassHash(), user.getSkills(), oldUserName, oldHash);
        try {
            this.databaseStatement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user.getUserName();
    }

    @Override
    public Object deleteUser(String userName) {
        /**
         *
         * //todo 暂不开放此功能
         */
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public String getSQL_CreateUser() {
        return SQL_CreateUser;
    }

    public String getSQL_QueryUser() {
        return SQL_QueryUser;
    }

    public String getSQL_UpdateUser() {
        return SQL_UpdateUser;
    }

    public String getSQL_DeleteUser() {
        return SQL_DeleteUser;
    }
}
